package de.idgard.android.addon.ui.services.impl;

import java.util.List;

import android.content.Context;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
import de.idgard.android.addon.annotation.ContainerInteraction;
import de.idgard.android.addon.ui.services.ZircoToIDGardAPI;
import de.idgard.android.utils.ReflectionUtils;

class ZircoToIDGardAPIImpl implements ZircoToIDGardAPI {

	@Override
	public boolean isMfindDialogVisible() {
		return (Boolean) ReflectionUtils.getDeclaredField(ctx,
				"mFindDialogVisible");
	}

	@Override
	public void addNewTab() {
		ViewFlipper vf = getViewFlipper();
		addTab(false, vf.getDisplayedChild());
	}

	@Override
	public void closeFindDialog() {
		ReflectionUtils.invokeMethod(ctx, "closeFindDialog", null, null);
	}

	@ContainerInteraction
	@Override
	public LinearLayout getUrlBar() {
		return (LinearLayout) ReflectionUtils.getDeclaredField(ctx, "mTopBar");
	}

	@ContainerInteraction
	@Override
	public boolean getUrlBarVisibility() {
		return (Boolean) ReflectionUtils
				.getDeclaredField(ctx, "mUrlBarVisible");
	}

	@ContainerInteraction
	@Override
	public void removeCurrentTab() {
		ReflectionUtils.invokeMethod(ctx, "removeCurrentTab", null, null);
	}

	@ContainerInteraction
	@Override
	public void updateBrowserUI() {
		ReflectionUtils.invokeMethod(ctx, "updateUI", null, null);
	}

	@ContainerInteraction
	@Override
	public void setCurrentWebView(WebView currentWebView) {
		ReflectionUtils
				.setDeclaredField(ctx, "mCurrentWebView", currentWebView);
	}

	@ContainerInteraction
	@Override
	public List<WebView> getContainersWebViews() {
		return (List<WebView>) ReflectionUtils.getDeclaredField(ctx,
				"mWebViews");
	}

	@ContainerInteraction
	@Override
	public void addTab(boolean navigateToHome, int parentIndex) {
		ReflectionUtils.invokeMethod(ctx, "addTab", new Object[] {
				navigateToHome, parentIndex }, new Class[] { boolean.class,
				int.class });
	}

	@ContainerInteraction
	@Override
	public ViewFlipper getViewFlipper() {
		ViewFlipper vf = (ViewFlipper) ReflectionUtils.getDeclaredField(ctx,
				"mViewFlipper");
		return vf;
	}

	@ContainerInteraction
	@Override
	public WebView getContainersCurrentView() {
		return (WebView) ReflectionUtils.getDeclaredField(ctx,
				"mCurrentWebView");
	}

	@ContainerInteraction
	@Override
	public void loadUrl(String url) {
		getContainersCurrentView().loadUrl(url);
	}

	@ContainerInteraction
	@Override
	public Boolean isLoading() {
		return (Boolean) ReflectionUtils.invokeMethod(
				getContainersCurrentView(), "isLoading", (Object[]) null,
				(Class[]) null);
	}

	@ContainerInteraction
	@Override
	public void stopLoading() {
		ReflectionUtils.invokeMethod(getContainersCurrentView(), "stopLoading",
				(Object[]) null, (Class[]) null);
	}

	@ContainerInteraction
	@Override
	public void navigateToUrl(String url) {
		ReflectionUtils.invokeMethod(ctx, "navigateToUrl",
				new Object[] { url }, new Class[] { String.class });
	}

	@ContainerInteraction
	@Override
	public void startToolbarsHideRunnable() {
		ReflectionUtils.invokeMethod(ctx, "startToolbarsHideRunnable",
				(Object[]) null, (Class[]) null);
	}

	private Context ctx;

	@Override
	public void setContext(Context ctx) {
		this.ctx = ctx;
	}

	@Override
	public void clearDownloads() {
		ReflectionUtils.invokeMethod(ctx, "clearDownloads", (Object[]) null,
				(Class[]) null);
	}

}
